﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraRichEdit.Services;
using DevExpress.XtraRichEdit;
using DevExpress.Utils;
using DevExpress.XtraRichEdit.Export;
using DevExpress.XtraRichEdit.Utils;
using System.IO;
using HtmlAgilityPack;

namespace CollegeShop.Repository.Win.DevHelper
{
    public class RichExporter : IUriProvider
    {
        int imageId;
        readonly RichEditControl control;
        List<LinkedAttachementInfo> attachments;

        public RichExporter(RichEditControl control)
        {
            Guard.ArgumentNotNull(control, "control");
            this.control = control;
        }

        /// <summary>
        /// 导出内容和嵌入资源
        /// </summary>
        /// <param name="htmlBody">HTML内容</param>
        /// <param name="attach">附件资源</param>
        public virtual void Export(out string htmlBody, out List<LinkedAttachementInfo> attach)
        {
            this.attachments = new List<LinkedAttachementInfo>();
            control.BeforeExport += OnBeforeExport;
            htmlBody = control.Document.GetHtmlText(control.Document.Range, this);
            HtmlDocument htmlDoc = new HtmlDocument();
            //输出成xml格式
            htmlDoc.OptionOutputAsXml = true;
            htmlDoc.LoadHtml(htmlBody);
            htmlBody = htmlDoc.DocumentNode.ChildNodes[2].ChildNodes[3].InnerHtml;
            control.BeforeExport -= OnBeforeExport;

            attach = this.attachments;
        }

        void OnBeforeExport(object sender, BeforeExportEventArgs e)
        {
            HtmlDocumentExporterOptions options = e.Options as HtmlDocumentExporterOptions;
            if (options != null)
            {
                options.Encoding = Encoding.UTF8;
            }
        }

        #region IUriProvider Members

        public string CreateCssUri(string rootUri, string styleText, string relativeUri)
        {
            return String.Empty;
        }

        public string CreateImageUri(string rootUri, RichEditImage image, string relativeUri)
        {
            string imageName = String.Format("image{0}", imageId);
            imageId++;

            RichEditImageFormat imageFormat = GetActualImageFormat(image.RawFormat);
            Stream stream = new MemoryStream(image.GetImageBytes(imageFormat));
            string mediaContentType = RichEditImage.GetContentType(imageFormat);
            LinkedAttachementInfo info = new LinkedAttachementInfo(stream, mediaContentType, imageName);
            attachments.Add(info);

            return "cid:" + imageName;
        }

        private RichEditImageFormat GetActualImageFormat(RichEditImageFormat _RichEditImageFormat)
        {
            if (_RichEditImageFormat == RichEditImageFormat.Exif ||
                _RichEditImageFormat == RichEditImageFormat.MemoryBmp)
                return RichEditImageFormat.Png;
            else
                return _RichEditImageFormat;
        }
        #endregion
    }
}
